package com.andnnl.template.param;

import java.util.List;
import java.util.Random;

import com.andnnl.template.ParamUtil;
import com.fasterxml.jackson.databind.JsonNode;

import cn.hutool.core.util.StrUtil;

/**
 * Created by chenss on 2025/7/8.
 */

// 枚举类型配置
public class EnumParamConfig extends BaseParamConfig {
    List<String> options;

    @Override
    public void validate() {
        if (options == null || options.isEmpty())
            throw new IllegalArgumentException("枚举选项缺失: " + name);
    }

    @Override
    public String generateValue() {
        Random rand = new Random();
        return options.get(rand.nextInt(options.size()));
    }

    public List<String> getOptions() {
        return options;
    }

    public void setOptions(List<String> options) {
        this.options = options;
    }

    @Override
    public void fromJson(JsonNode node) {
        JsonNode optionsNode = node.path("options");
        this.options = ParamUtil.parseList(optionsNode);
    }

    @Override
    public boolean validateValue(String value) throws IllegalArgumentException {
        if (StrUtil.isBlank(value)) {
            throw new IllegalArgumentException("枚举参数" + name + "不能为空");
        }
        if (!options.contains(value)) {
            throw new IllegalArgumentException("枚举参数" + name + "必须是以下选项之一: " + String.join(", ", options) + "，实际值：" + value);
        }
        return true;
    }
}
